<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部搜索 -->
        <el-row align="center" :gutter="20" type="flex" justify="space-around">
          <el-col :span="24">
            <el-form
              :inline="true"
              :model="pages"
              class="demo-form-inline"
              label-width="80px"
              size="normal"
            >
              <el-form-item label="关键字">
                <el-input
                  v-model="pages.keyword"
                  placeholder="根据文章标题搜索"
                  size="small"
                >
                </el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select
                  v-model="pages.state"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="cleardel">清除</el-button>
                <el-button size="small" type="primary" @click="onSubmit"
                  >搜索</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4">
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              class="btn"
              @click="Add"
              >新增技巧</el-button
            >
          </el-col>
        </el-row>
        <!-- 数据提示条 -->
        <div class="dig">
          <span class="el-icon-info">&nbsp;&nbsp;&nbsp;</span>数据一共{{
            total
          }}条
        </div>
        <!-- 表单  -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            fixed
            type="index"
            prop="date"
            label="序号"
            width="80px"
          >
          </el-table-column>
          <el-table-column prop="title" label="文章标题" width="400px">
            <template #default="{ row }">
              {{ row.title }}
              <el-link v-if="row.videoURL" @click="lookBtn(row)" :underline="false">
                <i class="el-icon-film" style="font-size: 18px;"></i>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数" align="center">
          </el-table-column>
          <el-table-column prop="username" label="录入人" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="录入时间" align="center">
            <template #default="{ row }">
              {{ formDate(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template #default="{ row }">
              {{ changeData(row.state) }}
            </template>
          </el-table-column>
          <el-table-column prop="ope" label="操作" width="200" align="center">
            <!-- 功能 -->
            <template #default="{ row }">
              <el-button type="text" @click="preview(row)">预览</el-button>
              <el-button type="text" @click="jinyong(row)">{{
                row.state ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                :disabled="Boolean(row.state)"
                @click="updateArticles(row)"
                >修改</el-button
              >
              <el-button
                type="text"
                :disabled="Boolean(row.state)"
                @click="deleteArticles(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row type="flex" justify="end" :gutter="20">
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pages.page"
              :page-size="pages.size"
              :page-sizes="pageSizes"
              layout="prev, pager, next,sizes,jumper"
              :total="total"
            >
            </el-pagination>
          </div>
        </el-row>
      </el-card>
      <!-- 视频 -->
      <div class="videomasks" v-if="videoSrc">
        <div class="closebotton" @click="closeVideo">
          <span class="el-icon-close"></span>
        </div>
        <div class="videobox">
          <video autoplay :src="videoSrc" controls ></video>
        </div>

      </div>
    </div>
    <!-- 预览弹框 -->
    <articlesPreview
      :dialogVisible="dialogVisible"
      @closegb="closegb"
      :previewData="previewData"
      :yulancishu="yulancishu"
    />
    <!-- 新增弹框 -->
    <articlesAdd
      :dialogVisibleAdd="dialogVisibleAdd"
      @guanbi="guanbi"
      @addForm="addForm"
      ref="updateForm"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, detail, changeState, remove } from '@/api/hmmm/articles.js'
import articlesAdd from '@/module-hmmm/components/articles-add'
import articlesPreview from '@/module-hmmm/components/articles-preview'
export default {
  components: {
    articlesAdd,
    articlesPreview
  },
  data () {
    return {
      // 表格数据
      tableData: [],
      // 表格数组暂存
      currentData: [],
      // 数据总数
      total: null,
      videoSrc: '',
      yulancishu: 0,
      look: false,
      previewData: {}, // 预览详情
      // 弹框显示
      dialogVisibleAdd: false,
      dialogVisible: false,
      // 分页参数
      pages: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null,
        id: '',
        username: ''
      },
      id: '',
      pageSizes: [5, 10, 20]
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 新增
    Add () {
      this.dialogVisibleAdd = true
    },
    // 关闭新增
    guanbi () {
      this.dialogVisibleAdd = false
    },
    // 新增后刷新列表
    addForm () {
      this.getList()
    },
    // 清除输入框
    cleardel () {
      this.pages.keyword = ''
      this.pages.state = null
      this.getList()
    },
    // 过滤表单数据
    changeData (val) {
      if (val === 0) {
        return '已禁用'
      } else if (val === 1) {
        return '已启用'
      }
    },
    // 页码变化
    handleSizeChange (page) {
      // console.log('页码改变')
      this.pages.pagesize = page
      this.getList()
    },
    // 当前页变动时候触发
    async handleCurrentChange (page) {
      // console.log('页面')
      this.pages.page = page
      this.getList()
    },
    // 获取文章列表
    async getList () {
      const res = await list(this.pages)
      this.tableData = res.data.items
      this.total = res.data.counts
      this.pages.username = res.data.username
      // console.log('列表数据', res)
    },
    // 搜索
    onSubmit () {
      this.pages.page = 1
      this.getList()
      // console.log('点了!')
    },
    // 时间格式化
    formDate (value, str = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(value).format(str)
    },
    // 视频
    lookBtn (row) {
      // console.log(row)
      this.look = true
      this.videoSrc = row.videoURL
    },
    // 关视频
    closeVideo () {
      this.look = false
      this.videoSrc = ''
    },
    // 预览
    async preview (row) {
      this.dialogVisible = true
      const res = await detail(row)
      this.previewData = res.data
      this.yulancishu = row.visits
      // console.log(res)
    },
    // 关闭预览
    closegb () {
      this.dialogVisible = false
    },
    async jinyong (row) {
      row.state = row.state ? 0 : 1
      await changeState(row)
      this.$message.success('操作成功')
    },
    // 修改
    async updateArticles (row) {
      this.dialogVisibleAdd = true
      this.$refs.updateForm.updateForm(row)
      // console.log(row)
    },
    // 删除
    deleteArticles (row) {
      this.$confirm('删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message.success('删除文章成功')
        this.getList()
      }).catch(() => {})
    }

  }
}
</script>

<style scoped lang='scss'>
.dig {
  margin-bottom: 15px;
  background-color: #f4f5f5;
  color: #909399;
  font-size: 13px;
  padding: 11px 10px;
  border-radius: 4px;
}
.btn {
  float: right;
}
.el-table__row i {
    color: #00f;
    font-size: 18px;
}
.videomasks {
  position: fixed;
  left: 0;
  top: 70px;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .closebotton {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .videobox {
    width: 800px;
    height: 460px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
